
package com.admissions;

import com.admissions.dapan.DapAnDeThi;
import com.admissions.diemchuan.TraDiemChuan;
import com.admissions.diemthi.TraDiemThi;
import com.admissions.tuyensinh.TuyenSinh2012;
import com.admissions.tylechoi.TyLeChoi;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

/**
 * @author Bkav-DangNVb
 *
 */
public class AdmissionsActivity extends TabActivity implements TabHost.TabContentFactory {
    private TabHost mTabHost;

    private ImageView mHeaderIcon;

    private TextView mHeaderContent;

    private TextView mHeaderComment;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mHeaderIcon = (ImageView) findViewById(R.id.header_icon);
        mHeaderContent = (TextView) findViewById(R.id.header_content);
        mHeaderComment = (TextView) findViewById(R.id.header_comment);

        mTabHost = getTabHost();
        Resources res = getResources();
        Intent intent;

        // Tab Tuyen sinh 2012
        intent = new Intent().setClass(AdmissionsActivity.this, TuyenSinh2012.class);
        setupTab("Tuyển sinh", R.drawable.ic_tuyen_sinh_2012, intent);

        // Tab Thi trac nghiem
        intent = new Intent().setClass(AdmissionsActivity.this, TabThiTracNghiem.class);
        setupTab("Thi Tr.Nghiệm", R.drawable.icon, intent);

        // Tab dap an de thi
        intent = new Intent().setClass(AdmissionsActivity.this, DapAnDeThi.class);
        setupTab("ĐA đề thi", R.drawable.ic_dap_an_de_thi, intent);

        // Tab ty le choi
        intent = new Intent().setClass(AdmissionsActivity.this, TyLeChoi.class);
        setupTab("Tỷ lệ chọi", R.drawable.ic_ty_le_choi, intent);

        // Tab tra cuu diem thi
        intent = new Intent().setClass(AdmissionsActivity.this, TraDiemThi.class);
        setupTab("Tr.Điểm thi", R.drawable.ic_tra_diem_thi, intent);

        intent = new Intent().setClass(AdmissionsActivity.this, TraDiemChuan.class);
        setupTab("Tr.Điểm chuẩn", R.drawable.ic_tra_diem_chuan, intent);

        // Set current tab
        mTabHost.setCurrentTab(1);

        //Set size for a tab
        int count = mTabHost.getTabWidget().getChildCount();
        for (int i = 0; i < count; i++) {
            LayoutParams lp = mTabHost.getTabWidget().getChildAt(i).getLayoutParams();
            lp.width = res.getDimensionPixelOffset(R.dimen.tab_width);
            lp.height = res.getDimensionPixelOffset(R.dimen.tab_height);
        }
    }

    private void setupTab(final String tag, final int icon, final Intent myIntent) {
        View tabView = createTabView(mTabHost.getContext(), tag, icon);
        TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabView).setContent(myIntent);
        mTabHost.addTab(setContent);
    }

    private View createTabView(final Context context, final String text, final int icon) {
        View tabLayout = LayoutInflater.from(context).inflate(R.layout.single_tab, null);
        ImageView tabIcon = (ImageView) tabLayout.findViewById(R.id.tab_icon);
        TextView tabName = (TextView) tabLayout.findViewById(R.id.tab_name);
        tabName.setText(text);
        Bitmap bm = BitmapFactory.decodeResource(getResources(), icon);
        tabIcon.setImageBitmap(bm);
        return tabLayout;
    }

    @Override
    public View createTabContent(String tag) {
        final TextView tv = new TextView(this);
        tv.setText("Content for tab with tag " + tag);
        return tv;
    }

    public void setupHeader(Drawable icon, String headerContent, String headerComment) {
        if (icon != null) {
            mHeaderIcon.setVisibility(View.VISIBLE);
            mHeaderIcon.setImageDrawable(icon);
        } else {
            mHeaderIcon.setVisibility(View.GONE);
            mHeaderContent.setGravity(Gravity.CENTER_HORIZONTAL);
            mHeaderComment.setGravity(Gravity.CENTER_HORIZONTAL);
        }
        mHeaderContent.setText(headerContent.toUpperCase());
        mHeaderComment.setText(headerComment);
    }

    public interface Header {
        public void setupHeader();
    }
}
